<template>
	<view>
		<!-- 添加自定义的搜索组件 -->
		<my-search @click="gotoSearchHandler"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧的滑动区 -->
			<scroll-view class="left-scorll-view" scroll-y="true" :style="{height: windowHeight + 'px'}">
				<block v-for="(item, index) in categoryList" :key="index">
					<view :class="['left-scroll-view-item', index === active ? 'active': '']" @click="changeActiveHandler(index)">{{item.cate_name}}</view>
				</block>
			</scroll-view>
			<!-- 右侧的滑动区 -->
			<scroll-view scroll-y="true" :style="{height: windowHeight + 'px'}" :scroll-top="scrollTop">
				<view class="category-lv2" v-for="(item2, index2) in cateLevel2List" :key="index2">
					<view class="category-lv2-title" @click="gotoGoodsList(item2)">
						{{item2.cate_name}}
					</view>
					
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前设备的可用窗口大小
				windowHeight: 0,
				
				// 分类列表数据
				categoryList: [],
				// 表示激活项的索引
				active: 0,
				
				// 二级分类列表
				cateLevel2List: [],
				
				//当点击切换选项时，重置滚动条到头部
				scrollTop: 0,
			};
		},
		onLoad() {
			// 获取当前设备的信息，屏幕大小，品牌等等。。
			const sysInfo = uni.getSystemInfoSync()
			// console.log(sysInfo)
			this.windowHeight = sysInfo.windowHeight-50;
			
			// 获取分类列表数据
			this.getCategoryList();
		},
		methods: {
			getCategoryList() {
				this.categoryList = [
					{
						"cate_id": 1,
						"cate_name": "第一级标题1",
						"cate_level": 0,
						"cate_deleted": false,
						"cate_icon": "",
						"children": [
							{
								"cate_id": 3,
								"cate_name": "第二级标题1",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							},
							{
								"cate_id": 4,
								"cate_name": "第二级标题2",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							},
							{
								"cate_id": 5,
								"cate_name": "第二级标题3",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							}
						]
					},
					{
						"cate_id": 7,
						"cate_name": "第一级标题2",
						"cate_level": 0,
						"cate_deleted": false,
						"cate_icon": "",
						"children": [
							{
								"cate_id": 8,
								"cate_name": "第二个 第二级标题1",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							},
							{
								"cate_id": 9,
								"cate_name": "第二个 第二级标题2",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							},
							{
								"cate_id": 10,
								"cate_name": "第二个 第二级标题3",
								"image_src": '',
								"description": '',
								"cate_pid": 1,
								"cate_level": 1,
								"cate_deleted": false,
								"cate_icon": ""
							}
						]
					}
				]
				//获取第二级分类数据
				this.cateLevel2List = this.categoryList[0].children;
			},
			changeActiveHandler(index) {
				this.active = index;
				this.cateLevel2List = this.categoryList[index].children;
				//切换滚动条到头部
				this.scrollTop = this.scrollTop === 0? 1: 0;
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '../../subpkg/goods_detail/goods_detail?cid=' + item.cate_id
				})
			},
			
			gotoSearchHandler(){
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container {
	display: flex;
	.left-scorll-view {
		width: 120px;
		
		.left-scroll-view-item {
			background-color: #F7F7F7;
			line-height: 60px;
			text-align: center;
			font-size: 12px;
			
			&.active {
				background-color: #FFFFFF;
				position: relative;
				
				
				&::before {
					content: ' ';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #C00000;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}
	}
}

.category-lv2-title {
	font-size: 12px;
	font-weight: bold;
	text-align: center;
	padding: 15px 0;
}
</style>
